//
//  TopAlertView.m
//  CCTopAlertView
//
//  Created by mac on 2017/9/15.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "TopAlertView.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define kAlertViewHeight (iPhoneX ? 104.f : 84.f)

@interface TopAlertView(){
    
    CGFloat stayTime;
    BOOL    _haveImage;
    
}

@property (nonatomic , assign)NSInteger backCountDown;
@property (nonatomic , strong)NSTimer *backTimer;

@property (nonatomic , strong)UIImageView *imageV;
@property (nonatomic , strong)UILabel *titleLab;
@property (nonatomic , strong)UIButton  *cancel;

@end
@implementation TopAlertView

/**

 @param color 背景色
 @param setStayTime 停留时间
 @param imageStr 图片名
 @param titleStr 标题
 @param titleColor 标题字体颜色
 
 */
+ (void)SetUpbackgroundColor:(UIColor *)color andStayTime:(CGFloat)setStayTime andImageName:(NSString *)imageStr andTitleStr:(NSString *)titleStr andTitleColor:(UIColor *)titleColor andCancel:(BOOL)cancel{
    
    TopAlertView *alert = [[TopAlertView alloc]init];
    [alert setupView:color andStayTime:setStayTime andImageName:imageStr andTitleStr:titleStr  andTitleColor:titleColor andCancel:cancel];
    [[UIApplication sharedApplication].keyWindow addSubview:alert];
    
}

- (id)init{
   
  if (self = [super init]){
      
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, kAlertViewHeight);

       [self setupViews];
       [self startPosition];
       [self setDeclineAnimation];
   }
    return self;
}

- (void)setupViews
{
    self.imageV = [[UIImageView alloc]init];
    self.imageV.frame = CGRectMake(0, 0, 18, 18);
    self.imageV.center = CGPointMake(25, (self.frame.size.height)/2+20);
    [self addSubview: self.imageV];
    
    self.titleLab = [[UILabel alloc]init];
    if (shareData.haveImage) {
        self.titleLab.frame = CGRectMake(40, 30, self.frame.size.width - 30 - 30, self.frame.size.height-20);
        self.layer.shadowColor = [UIColor blackColor].CGColor;
        self.layer.shadowOpacity = 0.5;
        self.layer.shadowOffset = CGSizeMake(0, 3);
    }else{
        self.titleLab.frame = CGRectMake(10, 30, self.frame.size.width - 30 - 30, self.frame.size.height-20);
    }
    self.titleLab.font = [UIFont systemFontOfSize:iPhoneX ? 15 : 14];
    self.titleLab.numberOfLines = 0;
    [self addSubview:self.titleLab];
    
    self.cancel = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cancel.frame = CGRectMake(SCREEN_WIDTH - 30, 53, 16, 16);
    self.cancel.backgroundColor = [UIColor clearColor];
    [self.cancel setImage:[UIImage imageNamed:@"网络异常_关闭"] forState:UIControlStateNormal];
    [self.cancel addTarget:self action:@selector(touch) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.cancel];
}
- (void)setupView:(UIColor *)color andStayTime:(CGFloat)setStayTime andImageName:(NSString *)imageStr andTitleStr:(NSString *)titleStr andTitleColor:(UIColor *)titleColor andCancel:(BOOL)cancel
{
    stayTime = setStayTime;
    self.backgroundColor = color;
    self.imageV.image = [UIImage imageNamed:imageStr];
    self.titleLab.textColor = titleColor;
    self.titleLab.text = titleStr;
    if (!cancel) {
        [self.cancel removeFromSuperview];
    }
    
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touch)];
    [self addGestureRecognizer:singleTap];
}

-(void)touch
{
    [self setGoUpAnimation];
   
}

//初始位置
- (void)startPosition
{
    self.center = CGPointMake(SCREEN_WIDTH/2, -self.frame.size.height/2-20);
}

//最终位置
- (void)endPosition
{
    self.center = CGPointMake(SCREEN_WIDTH/2, kAlertViewHeight/2-20);
}

//下降
- (void)setDeclineAnimation
{
    
    [UIView animateWithDuration: 0.9 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.9 options:0 animations:^{
        // 放大
        [self endPosition];
        
    } completion:^(BOOL finished) {
        
        if (stayTime >= 0) {
            [self backTime];
        }
    }];
}

//上升
- (void)setGoUpAnimation
{
     [self.backTimer invalidate];
    
    [UIView animateWithDuration:0.5 animations:^{
        
        [self startPosition];
        
    } completion:^(BOOL finished) {
        
        [self removeSelf];
        
    }];
}


- (void)backTime

{
    
    //设置倒计时总时长
    [self.backTimer invalidate];
    self.backCountDown = stayTime;
    self.backTimer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(viewBackNSTimer) userInfo:nil repeats:YES];
    
}

//使用NSTimer实现倒计时
- (void)viewBackNSTimer

{
    
    self.backCountDown -- ;
    
    if (self.backCountDown == 0  || self.backCountDown < 0) {
        [self.backTimer invalidate];
        [self setGoUpAnimation];
    }
    
}

-(void)removeSelf
{
    [self removeFromSuperview];
}
@end
